var e = getApp(), a = e.requirejs("core"), t = e.requirejs("wxParse/wxParse"), i = e.requirejs("biz/diypage"), s = e.requirejs("jquery");


Page({

  /**
   * 页面的初始数据
   */
  data: {
    selector:0,
    // 头部信息
    headerList:[],
    // 内容信息
    contentList:[],
    // 图片
    image:[],
    // 头部banner
    headerbanner:[],
    // eessenceid:1,
    isfollow:false,
    isgood:0,
    bid:'',
    pid:'',
    good:'',
    iszan:[],
    goodnum:[]
  },
    // 接口要的参数
    QueryParams:{
      id:'',
      page:1,
      isbest:0
    },
// 全局板块bid
  bid:'',
  contList:[],
   // 总页数
   totalPages:1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    // console.log(options)
    this.QueryParams.id = options.id
    this.getHeaderList()
    // this.postFollow(this.QueryParams.id)
  },
  onShow:function(){

    // this.getHeaderList()
    // this.getgood(this.data.bid,this.data.pid,this.data.isgood)
  },
  // 获取版块信息
  getHeaderList(){
    a.get('sns/board_detail&id=&page=1&isbest=',this.QueryParams,(res) => {
      // console.log(res)
      let total = res.result.total*1
      const  pagesize = res.result.pagesize
      // console.log(pagesize);
      
      // 计算总页数
      this.totalPages=Math.ceil(total/pagesize)
      //  console.log(this.totalPages);
      let headerList = res.result.board
      let contentList=res.result.list
      let isfollow = headerList.isfollow
      if(isfollow==1){
        let isfollow=this.data.isfollow
        isfollow=true
        this.setData({
          isfollow
        })
      }
    //  console.log(this.data.isfollow);
     
      let image = contentList.map(v=>v.images)

      this.setData({
        headerList,
        contentList:[...this.data.contentList,...contentList],
        image,
        headerbanner :'https://www.changshengyuan.cn/attachment/'+headerList.banner
      })
      // console.log(this.data.contentList)
    })
     // 关闭下拉刷新
     wx.stopPullDownRefresh()
  },

    // 页面上拉刷新事件
  onReachBottom(){
    // console.log("上拉刷新");
    // 判断有没有下一页数据
    if(this.QueryParams.page>=this.totalPages){
      // console.log("没有下一页数据");
      wx.showToast({title:'没有更多话题'})
    }else{
      // console.log("有下一页数据");
      this.QueryParams.page++
      this.getHeaderList()
    }
   
  },
  
// 下拉刷新事件
onPullDownRefresh(){
  // console.log("刷新");
  this.setData({
    contentList:[]
  }),
  this.QueryParams.page=1,
  this.getHeaderList()
  // this.getgood(this.data.bid,this.data.pid,this.data.isgood)
},

  // 已关注/关注的切换
  checkHandler(){
    this.postFollow(this.QueryParams.id)
  },
  // 回复点击
  replayhandle(){
   this.bid=this.QueryParams.id
   wx.setStorageSync('bid', this.bid)

  },
  // 板块关注/已关注请求
 postFollow(bid){
  a.post('sns/board_follow',{bid},(res) => {
    // console.log(res, bid)
    let isfollow =res.result.isfollow
    
    this.setData({
      isfollow
    })
  })
  // console.log(this.data.isfollow);
  
 },

//  点赞功能
favorclick(e){
  // console.log(e);
  let bid = wx.getStorageSync('bid')
  let pid = e.currentTarget.dataset.id
  let isgood = this.data.isgood
  wx.setStorageSync('pid', pid)
  this.setData({
    bid,
    pid,
    isgood,
  })
  this.getgood(this.data.bid,this.data.pid,this.data.isgood)
  
},
// 点赞请求
  getgood(bid,pid,isgood){
    a.post('sns/board_like',{bid,pid,isgood},(res) => {
      // console.log(pid);
      let isgood =res.result.isgood
      // console.log(isgood);
      
      if(res.status==1){
        let contentList = this.data.contentList.map(v=>{
          if(v.id==pid){
            if(isgood==1){
              v.goodcount=v.goodcount*1+1
            }else if(isgood==0){
              v.goodcount=v.goodcount*1-1
            }
           }
          return v
         })
         this.setData({
          contentList
         })
        }
    })
   
  }
})